package com.view;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.adpter.ReceResumeListAdapter;
import com.android.ocean.util.common.StringUtil;
import com.android.ocean.view.XListView;
import com.android.ocean.view.XListView.IXListViewListener;
import com.common.method.FromFlag;
import com.http.UserResumeFilterTask;
import com.matang.R;
import com.modle.UserResumeWrapper;
import com.util.AppUtil;

/**
 * 
 * @ClassName:XReceResumeListView
 * @Description:TODO用户收到的简历，可以直接填充数据，也可以传入条件后网络筛选
 * @author zhuw
 * @date 2013-8-25 下午3:43:50
 */
public class XReceResumeListView extends BaseFrameLayout implements
		IXListViewListener, UserResumeFilterTask.CallBack {
	ArrayList<UserResumeWrapper> dataList = new ArrayList<UserResumeWrapper>();
	int currentPage = 1;
	ReceResumeListAdapter adapter;
	XListView xlistView;
	HashMap<String, Object> conditionMap;

	public XReceResumeListView(Context context) {
		super(context);
		init(context);
	}

	public XReceResumeListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public XReceResumeListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	private void init(Context ctx) {
		this.context = ctx;
		View view = LayoutInflater.from(ctx).inflate(
				R.layout.l_merge_xlist_conlist, this);
		xlistView = (XListView) view.findViewById(R.id.con_listview);
		xlistView.setPullLoadEnable(false);
		xlistView.setPullRefreshEnable(false);
		xlistView.setXListViewListener(this);
		xlistView.setRefreshTime(StringUtil.getFreshTime());
		loadingView = (View) view.findViewById(R.id.loading_view);
		xlistView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
				Bundle b = new Bundle();
				b.putSerializable("userID", dataList.get(pos - 1).rStruct.userID);
				b.putInt("fromFlag", FromFlag.USER_RESUME_F_RECEIVE_RESUME);
			//	TranHelper.toAct(context, UserResumeInfoActivity.class, b);

			}
		});

	}

	/**
	 * 
	 * @Description:TODO传入数据列表，直接填充数据；
	 * @param@param dataList
	 * @return void
	 * @author zhuw
	 * @date 2013-8-25 下午3:46:52
	 */
	public void initData(ArrayList<UserResumeWrapper> dataList) {
		flushData();
		this.dataList = dataList;
		fillList();
	}

	public void initData(HashMap<String, Object> conditionMap) {
		flushData();
		this.conditionMap = conditionMap;
		loadConList();
	}

	private void fillList() {
		if (StringUtil.isListNull(dataList)) {
			AppUtil.tsMsg(context, "无数据");
		} else {
			adapter = new ReceResumeListAdapter(dataList, context);
			xlistView.setAdapter(adapter);

		}

	}

	/**
	 * 加载数据
	 */
	private void loadConList() {
		showLoading(loadingView);
		new UserResumeFilterTask(this).execute(conditionMap);
	}

	/**
	 * 此控件可以复用，当再次initConlistCatID或刷新前时需要先清除数据
	 */
	private void flushData() {
		currentPage = 1;
		xlistView.setAdapter(null);
		xlistView.setPullLoadEnable(false);
		xlistView.setRefreshTime(StringUtil.getFreshTime());
		if(StringUtil.isListNull(dataList)){
			dataList.clear();
			
		}
		if (adapter != null) {
			adapter.notifyDataSetChanged();
		}
	}

	@Override
	public void onRefresh() {
	}

	@Override
	public void onLoadMore() {
		// currentPage++;
		// loadConList();
	}

	@Override
	public void procError(String msg) {
		disLoading(loadingView);
		AppUtil.tsMsg(context, msg);

	}

	@Override
	public void procUserResumeFilter(ArrayList<UserResumeWrapper> dataList) {
		disLoading(loadingView);
		this.dataList = dataList;
		fillList();

	}

}
